﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;
using Zvezda.FSO2.DB;

namespace FSO
{
    public partial class FormSearchProductUnit : Form
    {
        public enum ProductUnitSearchTypeEnum : byte
        {
            INVOICE_OUT = 1,
            PAYMENT_IN,
            VAT_INVOICE,
            CALENDAR_PLAN
        }

        public RadListDataItem[] dataSearchItems = {
                                                        new RadListDataItem("Исходящий счет") {Value = ProductUnitSearchTypeEnum.INVOICE_OUT},
                                                        new RadListDataItem("Входящий платеж") {Value = ProductUnitSearchTypeEnum.PAYMENT_IN},
                                                        new RadListDataItem("Счет-фактура"){Value = ProductUnitSearchTypeEnum.VAT_INVOICE},
                                                        new RadListDataItem("Календарный план"){Value =ProductUnitSearchTypeEnum.CALENDAR_PLAN}                                                        
                                                    };
        public FormSearchProductUnit()
        {
            InitializeComponent();

            radDropDownListSearchItems.Items.Clear();

            foreach (Telerik.WinControls.UI.RadListDataItem item in dataSearchItems)
            {
                radDropDownListSearchItems.Items.Add(item);
            }
            
        }

        public void DesignMultiLineomboBoxItems(ProductUnitSearchTypeEnum index)
        {
            radMultiColumnComboBoxSearchProduct.AutoSizeDropDownToBestFit = true;
            RadMultiColumnComboBoxElement multiColumnComboElement = radMultiColumnComboBoxSearchProduct.MultiColumnComboBoxElement;
            //multiColumnComboElement.DropDownSizingMode = SizingMode.UpDownAndRightBottom;

            switch (index)
            {
                case ProductUnitSearchTypeEnum.INVOICE_OUT:
                    {

                    }
                    break;
                case ProductUnitSearchTypeEnum.PAYMENT_IN:
                    {

                    }
                    break;
                case ProductUnitSearchTypeEnum.VAT_INVOICE:
                    {

                    }
                    break;
                case ProductUnitSearchTypeEnum.CALENDAR_PLAN:
                    {
                        radMultiColumnComboBoxSearchProduct.DataSource = null;
                        view_SEARCH_CALENDAR_PLANTableAdapter1.Fill(dataSetFSO1.View_SEARCH_CALENDAR_PLAN);                                             
                        RadGridView gridViewControl = multiColumnComboElement.EditorControl;
                        gridViewControl.MasterTemplate.AutoGenerateColumns = false;
                        gridViewControl.ReadOnly = true;
                        gridViewControl.Columns.Add(new GridViewTextBoxColumn(dataSetFSO1.View_SEARCH_CALENDAR_PLAN.NUMBERColumn.ColumnName) { HeaderText = "№" });
                        gridViewControl.Columns.Add(new GridViewDateTimeColumn(dataSetFSO1.View_SEARCH_CALENDAR_PLAN.START_DATEColumn.ColumnName) { HeaderText = "Дата начала" });
                        gridViewControl.Columns.Add(new GridViewDateTimeColumn(dataSetFSO1.View_SEARCH_CALENDAR_PLAN.END_DATEColumn.ColumnName) { HeaderText = "Дата окончания" });
                        gridViewControl.Columns.Add(new GridViewTextBoxColumn(dataSetFSO1.View_SEARCH_CALENDAR_PLAN.CONTRACT_NUMBERColumn.ColumnName) { HeaderText = "№ договора" });
                        radMultiColumnComboBoxSearchProduct.DataSource = dataSetFSO1.View_SEARCH_CALENDAR_PLAN;
                        radMultiColumnComboBoxSearchProduct.DisplayMember = dataSetFSO1.View_SEARCH_CALENDAR_PLAN.NUMBERColumn.ColumnName;
                        radMultiColumnComboBoxSearchProduct.ValueMember = dataSetFSO1.View_SEARCH_CALENDAR_PLAN.IDColumn.ColumnName;                                                
                    }
                    break;
                default:
                    break;
            }

        }

        public void DesignProductUnitGrid(ProductUnitSearchTypeEnum index)
        {
            int itemId = -1;

            radGridViewProductUnits.ReadOnly = true;
            radGridViewProductUnits.EnableGrouping = false;
            radGridViewProductUnits.EnableFiltering = true;
            radGridViewProductUnits.MasterTemplate.EnableSorting = true;

            GridViewTextBoxColumn colProdName = new GridViewTextBoxColumn(dataSetFSO1.ViewSearchProductUnit.PRODUCT_UNIT_NAMEColumn.ColumnName);
            colProdName.HeaderText = "Наименование";            

            GridViewDecimalColumn colCount = new GridViewDecimalColumn(dataSetFSO1.ViewSearchProductUnit.COUNTColumn.ColumnName);
            colCount.HeaderText = "Количество";
            colCount.DecimalPlaces = 0;            

            GridViewTextBoxColumn colMeter = new GridViewTextBoxColumn(dataSetFSO1.ViewSearchProductUnit.METER_NAMEColumn.ColumnName);
            colMeter.HeaderText = "Ед. Измерения";            

            GridViewCheckBoxColumn colType = new GridViewCheckBoxColumn(dataSetFSO1.ViewSearchProductUnit.PRODUCT_UNIT_TYPEColumn.ColumnName);
            colType.HeaderText = "Товар";

            radGridViewProductUnits.MasterTemplate.Columns.Clear();
            radGridViewProductUnits.MasterTemplate.Columns.Add(colProdName);
            radGridViewProductUnits.MasterTemplate.Columns.Add(colCount);
            radGridViewProductUnits.MasterTemplate.Columns.Add(colMeter);
            radGridViewProductUnits.MasterTemplate.Columns.Add(colType);

            SortDescriptor type = new SortDescriptor(dataSetFSO1.ViewSearchProductUnit.PRODUCT_UNIT_TYPEColumn.ColumnName, ListSortDirection.Descending);
            SortDescriptor name = new SortDescriptor(dataSetFSO1.ViewSearchProductUnit.PRODUCT_UNIT_NAMEColumn.ColumnName, ListSortDirection.Ascending);
            radGridViewProductUnits.SortDescriptors.Add(type);
            radGridViewProductUnits.SortDescriptors.Add(name);

            switch (index)
            {
                case ProductUnitSearchTypeEnum.INVOICE_OUT:
                    {

                    }
                    break;
                case ProductUnitSearchTypeEnum.PAYMENT_IN:
                    {

                    }
                    break;
                case ProductUnitSearchTypeEnum.VAT_INVOICE:
                    {

                    }
                    break;
                case ProductUnitSearchTypeEnum.CALENDAR_PLAN:
                    {
                        itemId = ((DataSetFSO.View_SEARCH_CALENDAR_PLANRow)((DataRowView)((GridViewDataRowInfo)radMultiColumnComboBoxSearchProduct.SelectedItem).DataBoundItem).Row).ID;
                        viewSearchProductUnitTableAdapter1.FillByCalendarPlanId(dataSetFSO1.ViewSearchProductUnit, itemId);
                    }
                    break;
                default:
                    break;
            }           

            radGridViewProductUnits.DataSource = dataSetFSO1.ViewSearchProductUnit;
        }

        #region Events

        private void radDropDownListSearchItems_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            if (radDropDownListSearchItems.SelectedItem != null && radDropDownListSearchItems.SelectedItem.Value != null)
            {
                DesignMultiLineomboBoxItems((ProductUnitSearchTypeEnum)radDropDownListSearchItems.SelectedItem.Value);
            }
        }

        

        private void radMultiColumnComboBoxSearchProduct_SelectedIndexChanged(object sender, EventArgs e)
        {           
            if (radMultiColumnComboBoxSearchProduct.SelectedItem != null)
            {                
                DesignProductUnitGrid((ProductUnitSearchTypeEnum)radDropDownListSearchItems.SelectedItem.Value);
            }
        }

        #endregion
    }
}
